<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use DB;
use Hash;
use Mail;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class FindController extends Controller
{
    public function getFind()
    {
        return view('home.login.find');
    }

    public function find(Request $request)
    {
        // dd($request->all());
        //处理数据
        $email = $request->input('email');
        $data = DB::table('hs_member')->where('email',$email)->select('id','token')->first();
        //判断邮箱是否正确
        if ($data) {

            $this->sendsMail($data->id,$data->token,$email);
           
            return view('home.login.zhaohui',['id'=>$data->id]);
        }else{
            return back()->with('error','邮箱不存在,请重新输入正确的邮箱');
        }
    }

    //邮箱激活
    public function sendsMail($id,$token,$email)
    {
        // echo $id;
        // echo $token;
        // echo $email;
        // 
        //发送原始字段形式的邮件
        // Mail::raw('您在红薯网的密码已经修改了,请本人进行核实', function ($message) {
        //     //设置邮件标题
        //     $message->subject('激活邮件');
        //     //发送给谁
        //     $message->to('$email');
        // });

        
        //发送其它形式的邮件
        Mail::send('mail.rh', ['id'=>$id,'token'=>$token], function ($message)use($email) {
            $message->to($email)->subject('激活邮件');
        });

        // return view('mail.rh');
    }

    public function zhaohui(Request $request)
    {

        $this->validate($request, [
            'password' => 'required|max:18|min:6',
            'repassword' => 'required|same:password',
        ],[
            'password.required'=>'密码不能为空',
            'password.min'=>'密码长度不够,最小6位',
            'password.max'=>'密码长度太长,最多18位',
            'repassword.required'=>'确认密码不能为空',
            'repassword.same'=>'确认密码和密码填写的不一样',
        ]);
        $password = $request->input('password');
        $id = $request->input('id');  
        // dd($id);
        //密码加密
        $data['password'] = Hash::make($password);

        $user = DB::table('hs_member')->where('id',$id)->update($data);
        $testing = DB::table('hs_member')->where('id',$id)->select('testong')->first();
        if ($user) {
            if($testing == 2){
                return view('home.login.login')->with('success','您已修改密码成功请登录');
            }
        }else{
            return back()->with('error','您修改密码失败');
        }
    }

    //邮箱密码修改验证
    public function dofind(Request $request)
    {
         $id = $request->input('id');

        //根据id查询用户
        $user = DB::table('hs_member')->where('id',$id)->first();
        //请求是否正确
        if($user->token == $request->input('token')){
            $data['testing'] = 2;
            $data['token'] = str_random(50);
            $res = DB::table('hs_member')->where('id',$id)->update($data);
            if($res){
                return redirect('/home/login/login')->with('success','修改成功');
            }else{
                return redirect('/find/find')->with('error','修改失败');
            }

        }else{
            return redirect('/find/find')->with('error','激活码失效');
        }
    }
}